<%@ page import="com.weaver.general.Util" %>
<%@ page import="static com.weavernorth.util.dateUtils.getNowDateTime" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.weavernorth.fadada.util.fadadaUtil" %>
<%@ page import="com.weavernorth.util.MyCrypUtils" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%
    String strSign = "";
    String timestamp = "";
    String biz_content = "";
    String str_response = "";
    String inStr="";
    String isSubmit = Util.null2String(request.getParameter("isSubmit"));
    //region [公共参数]
    timestamp = getNowDateTime();
    //配置接口路径
    HashMap<String, String> urlMap = new HashMap<>();
    urlMap.put("账户注册", "/account/register");
    urlMap.put("创建合同模板", "/contract/template/save");
    urlMap.put("查询模板", "/online-template/company/list");
    urlMap.put("模板填充", "/contract/generate");
    urlMap.put("查询客户编号", "/company/customer-id/list");
    urlMap.put("文件上传", "/file/upload");
    String cType = "文件上传";
    //endregion
    if(isSubmit.equals("submit")) {

        if (cType.equals("文件上传")) {
            String myfile = Util.null2String(request.getParameter("uploadFile"));//文件
            biz_content = fadadaUtil.uploadFile(".pdf","1");//文件上传
            inStr = "appId=" + fadadaUtil.appId + "&bizContent=" + biz_content + "&signType=" + fadadaUtil.signType + "&timestamp=" + timestamp;//待签名字符串
            try {
                strSign = MyCrypUtils.sh256Encrypt(inStr).toUpperCase();
                strSign = MyCrypUtils.sh256Encrypt(strSign + fadadaUtil.appkey).toUpperCase();
                strSign = MyCrypUtils.Base64Encode((strSign).getBytes());
            } catch (Exception ex) {
                System.out.println("sha256异常,异常信息：" + ex.toString());
            }

            HashMap<String, Object> params = fadadaUtil.getParams(timestamp,strSign, biz_content);
            params.put("file",myfile);
            System.out.println("参数：" + params.toString());
            str_response = fadadaUtil.sedDataTofdd(urlMap.get("文件上传"), params);
            System.out.println("文件上传结果：" + str_response);
        }
    }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>文件上传</title>
    <script src="/js/jquery/jquery_wev8.js"></script>
    <script type="text/javascript">
        function checkSubmit() {
            var fileSize=$("#uploadFile")[0].files[0].size;
            var newFileSize=Math.round(fileSize/(1024*1024));
            console.log("上传的文件大小："+newFileSize+"M");
            alert("文件："+frm.uploadFile.value);
            if(newFileSize>40){
                alert("上传的文件过大,上传上限为40M");
                return;
            }
            frm.isSubmit.value="submit";
            frm.submit();
        }
    </script>
</head>
<body>
<h1>文件上传</h1>
<form name="frm" method="post" action="uploadFile.jsp" >
    <input type="hidden" name="isSubmit" value="">
    选择一个文件:
    <input type="file" name="uploadFile" id="uploadFile"/>
    <br/><br/>
    <input type="submit"  value="上传" onclick="checkSubmit()"/>
    <div>
        接口返回数据：<%=str_response%>
    </div>
</form>
</body>
</html>